<?php
/*
 * Created on 23.04.2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 class Model_Basket extends Models_Base {
 	
// 	static private $basket = array();
 	//$this->user->id, $_POST[id], $_POST[quantity], $_POST[sizes] , $_POST[colors], $_POST[sizes], $goods->find(array(id => $_POST[id]))->price
 	function add($user_id, $good_id, $quantity, $sizes,  $colors,  $price, $in_suit = 0) {
 		
 		
 		error_log("Price: $price <=");
 	
// 		self::$basket[$user_id][$good_id] = $quantity;
 		$_SESSION['BASKET'][$user_id][$good_id][quantity] 	= $quantity;
 		$_SESSION['BASKET'][$user_id][$good_id][price]		= $price;
 		$_SESSION['BASKET'][$user_id][$good_id][colors]		= $colors;
 		$_SESSION['BASKET'][$user_id][$good_id][sizes]		= $sizes;
 		$_SESSION['BASKET'][$user_id][$good_id][in_suit]	= $in_suit;
 		
 	}
 	
	function get($user_id) {
		
		return $_SESSION['BASKET'][$user_id] ? $_SESSION['BASKET'][$user_id] : array();
//		return self::$basket;
	}
	
	function quantity($user_id) {
		$QUANTIY = null;
		
//		print_r/
		
		foreach($this->get($user_id) as $key => $value) {
			$QUANTIY += $value[quantity] * count($value[colors]) * $value[sizes];
		}		
		
		return $QUANTIY;
	}
	
	function total($user_id) {
		
		$TOTAL = null;
		
		foreach($this->get($user_id) as $key => $value) {
			$TOTAL += $value[price] *  $value[quantity] * count($value[colors]) * $value[sizes];
		}			
		
		return $TOTAL;
	}
	
	function clear($user_id) {
		unset($_SESSION['BASKET'][$user_id]);
	}
	
	function delete($user_id, $good_id) {
		unset($_SESSION['BASKET'][$user_id][$good_id]);
	} 
	
	function is_empty($user_id) {
		
//		print_r($_SESSION['BASKET'][$user_id]);
		error_log('is_empty called');
		return $_SESSION['BASKET'][$user_id] ? 0 : 1;
	}
	
	
	
	
	function getIds($user_id) {
		foreach($this->get($user_id) as $key => $value) {
			$out[] = array(id => $key,quantity => $value[quantity], colors => $value[colors], sizes => $value[sizes], in_suit => $value[in_suit]);
		} 
		
		return $out;
	}
	
	function getList() {
			
		foreach ($_SESSION['BASKET'] as $key => $value) {
			$out[] = array(
				uid 	=> $key,
				value	=> $value
			);
		}
		
		return $out;
	}
 	
 }
?>
